package com.xzj.admin.resource.analysis.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.domain.ResMessageManage;
import com.xzj.admin.resource.analysis.domain.constants.AnalysisConstants;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: xzj-admin
 * @ClassName: IdConvertUtil
 * @description: ID映射处理方法
 * @author: XiaZunJun
 * @create: 2024-10-28 17:37
 */
@Slf4j
public class IdConvertUtil {
    /**
     * 公用转换换方法
     *
     * @param resMessageManage 消息实体
     * @author xiazunjun
     * @date 2023/8/16 16:14
     */
    public static void commonIdConvert(ResMessageManage resMessageManage) {
        commonConvertId(resMessageManage, "id");
    }

    public static void commonConvertId(ResMessageManage resMessageManage, String... valueKeys) {
        JSONObject rowData = resMessageManage.getRowData();
        String messageId = null;
        for (String valueKey : valueKeys) {
            if (StrUtil.isNotEmpty(rowData.getString(valueKey))) {
                if (null != messageId) {
                    messageId = commonBuildStr(messageId, rowData.getString(valueKey));
                } else {
                    messageId = rowData.getString(valueKey);
                }
            }
        }
        if (null != messageId) {
            resMessageManage.setMessageId(messageId);
        }
    }

    /**
     * 功能根据入参拼接相应字符串
     *
     * @param params 需用通用组装的字段集合
     * @return String
     * @author xiazunjun
     * @date 2023/8/16 15:09
     */
    public static String commonBuildStr(String... params) {
        return buildByJoinChar(AnalysisConstants.DEFAULT_JOIN_SPLIT, params);
    }

    public static String buildByJoinChar(String joinChar, String... params) {
        StringBuilder strBuilder = new StringBuilder();
        for (String param : params) {
            strBuilder.append(param).append(joinChar);
        }
        String redisKey = strBuilder.toString();
        return redisKey.substring(0, redisKey.length() - 1);
    }
}
